package com.asiainfo.wx.config;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Copyright: Copyright (c) 2019  zteits
 *
 * @ClassName: com.asiainfo.wx.config
 * @Description: spring web mvc 配置信息
 * @version: v1.0.0
 * @author: atao
 * @date: 2019-10-22   23:47
 * Modification History:
 * Date         Author          Version      Description
 * ---------------------------------------------------------*
 * 2019-10-22      atao          v1.0.0          创建
 */
@Configuration
public class SpringMvcConfig implements WebMvcConfigurer {


    /**
     * 设置ajax返回的json 序列化方式，采用 fastjson 进行序列化
     * @param
     * @return
     * @Author: atao
     * @Date: 2019-10-23 00:04
     *
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters){
        //采用fastjson 转换消息
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig jsonConfig = new FastJsonConfig();
        jsonConfig.setCharset(Charset.forName("utf-8"));
        /**json禁用循环依赖:json串返回前端有：$ref.data格式数据.*/
        jsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);
        //jsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        fastJsonHttpMessageConverter.setFastJsonConfig(jsonConfig);
        List<MediaType> mediaTypes = new ArrayList();
        mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastJsonHttpMessageConverter.setSupportedMediaTypes(mediaTypes);
        converters.add(fastJsonHttpMessageConverter);


    }
}
